package elcom.model.data;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import elcom.model.common.Log;
import elcom.model.connection.DBConnection;

public class TransferData implements ITransferData {

	@Override
	public ArrayList<Student> getData(Integer rownum) {
		if (rownum == null || rownum == 0) {
			rownum = 2000;
		}
		ArrayList<Student> lstStudent = new ArrayList<>();
		Connection con = DBConnection.getConnection();
		String sql = "select p_student_id from p_student where isused = 'N' and rownum <= ?";
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = con.prepareStatement(sql);
			ps.setInt(1, rownum);
			rs = ps.executeQuery();

			while (rs.next()) {
				Integer studentID = rs.getInt(1);
				// System.out.println("==>" +p_Student_ID);
				Student student = new Student(studentID);
				lstStudent.add(student);
			}
		} catch (Exception e) {
			e.printStackTrace();
			Log.logErr(e.toString());
		} finally {
			try {
				if (rs != null) {
					rs.close();
				}
				if (ps != null) {
					ps.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
				Log.logErr(e.toString());
			}
		}
		return lstStudent;
	}

	@Override
	public void updateData(List<Student> lstStudent) {
		for (int i = 0; i < lstStudent.size(); i++) {
			Student student = lstStudent.get(i);
			student.updateIsUsed();
		}
	}

}
